#pragma once

#include <iostream>
#include <string>
// #include <jsoncpp/json/json.h>
#define MySelf 1
//协议——>    5\n1 + 1\n     1 + 1是内容  5是字节大小

const std::string blank_space_sep=" ";
const std::string protocol_sep="\n";

//打包   传入 内容1 + 1
std::string Encode(const std::string &content)
{
    std::string package=to_string(content.size());
    package+=protocol_sep;
    package+=content;
    package+=protocol_sep;
    return package;
}

//解包   报文不是只有一帧  假设报文是5\n1 + 1\n
bool Decode(std::string &package,std::string&content)
{
    //返回即 protocol_sep 起始字符的下标
    std::size_t pos=package.find(protocol_sep);  
    if(pos == std::string::npos) return false;
    std::string len_str=package.substr(0,pos);  // 字符5
    int len=std::stoi(len_str.c_str());   //求出内容长度5
    int total_len=len_str.size()+1+len+1;   //1+1  +5  +1  =8
    //剩下的内容不够一帧数据
    if(package.size()<total_len)
    {
        printf("Decode error  \n  ");
        return false;
    }
        

    content=package.substr(pos+1,len);
    //处理完这个后，移除
    package.erase(0,total_len);
    return true;
}


//序列化类
class Sequent
{
public:
    Sequent(int x,char op,int y)
    :_x(x)
    ,_op(op)
    ,_y(y)
    {}
    Sequent()
    {}

    ~Sequent()
    {}
    //序列化是将操作数集合到一起   1 + 1
    void Serialize(std::string &content)
    {
        std::string str;
        str=to_string(_x);
        printf("Serialize::%s,x  : %d  y:  %d  op:%c\n",str.c_str(),_x,_y,_op);
        str+=blank_space_sep;
        str+=_op;
      printf("Serialize::%s,x  : %d  y:  %d  op:%c\n",str.c_str(),_x,_y,_op);
        str+=blank_space_sep;
      printf("Serialize::%s,x  : %d  y:  %d  op:%c\n",str.c_str(),_x,_y,_op);
        str+=to_string(_y);
        content=str;
        printf("Serialize::%s,x  : %d  y:  %d  op:%c\n",str.c_str(),_x,_y,_op);
    }
    //反序列化：提取操作数
    bool DeSerialize(std::string&str)
    {
        int pos_left=str.find(blank_space_sep);
        if(pos_left==std::string::npos) { printf("DeSerialize error1  \n  "); return false;}
        int pos_right=str.rfind(blank_space_sep);
        if(pos_right==std::string::npos) { printf("DeSerialize error2  \n  ");return false;}

        std::string op=str.substr(pos_left+1,pos_right-pos_left-1);
        std::string x=str.substr(0,pos_left);
        std::string y=str.substr(pos_right+1);     //
        _x=std::stoi(x);
        _y=std::stoi(y);
        _op=op[0];
        return true;
    }
public:
    int _x;
    int _y;
    char _op;
};


class Response
{
public:
    Response(int res, int c) : result(res), code(c)
    {
    }

    Response()
    {}
public:
    bool Serialize(std::string &out)
    {
#ifdef MySelf
        // "result code"
        // 构建报文的有效载荷
        std::string s = std::to_string(result);
        s += blank_space_sep;
        s += std::to_string(code);
        out = s;
        return true;
#else
        Json::Value root;
        root["result"] = result;
        root["code"] = code;
        // Json::FastWriter w;
        Json::StyledWriter w;
        *out = w.write(root);
        return true;
#endif
    }
    bool Deserialize(const std::string &in) // "result code"
    {
#ifdef MySelf
        std::size_t pos = in.find(blank_space_sep);
        if (pos == std::string::npos)
            return false;
        std::string part_left = in.substr(0, pos);
        std::string part_right = in.substr(pos+1);

        result = std::stoi(part_left);
        code = std::stoi(part_right);

        return true;
#else
        Json::Value root;
        Json::Reader r;
        r.parse(in, root);

        result = root["result"].asInt();
        code = root["code"].asInt();
        return true;
#endif

    }
    void DebugPrint()
    {
        std::cout << "结果响应完成, result: " << result << ", code: "<< code << std::endl;
    }
public:
    int result;
    int code; // 0，可信，否则!0具体是几，表明对应的错误原因
};


